package messageUtils;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import utils.Constant;

public class MessageSend
{
    public static MessageResult postMessageChina(SMSMessage sms){
        HttpClient client = HttpClients.createDefault();
        MessageResult.Builder resBuilder = new MessageResult.Builder();
        HttpPost post = null;   
        try
        {           
            MessageConfig config = MessageConfig.getConfig();
            post = new HttpPost(config.getUrl());
            post.addHeader("Content-Type",config.getContentType());
            List<NameValuePair> data = new ArrayList<NameValuePair>();
            data.add(new BasicNameValuePair("Uid", config.getAppUid()));
            data.add(new BasicNameValuePair("Key", config.getAppKey()));
            data.add(new BasicNameValuePair("smsMob",sms.getMessageTo()));
            data.add(new BasicNameValuePair("smsText",sms.getMessageContext()));
            post.setEntity(new UrlEncodedFormEntity(data,"utf-8")); 
            HttpResponse response = client.execute(post);
            Header[] headers = response.getAllHeaders();
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode:"+statusCode);
            for(Header h:headers){
                System.out.println(h.toString());
            }
            String result = EntityUtils.toString(response.getEntity());
            
            if (Integer.valueOf(result)>0)
            {
                resBuilder.setStatusCode(Constant.OK);
            }else if (result.equals("-1")) {
                resBuilder.setMsg("没有该用户账户").setStatusCode(Constant.FAIL);
            }else if (result.equals("-2")) {
                resBuilder.setMsg("接口密钥不正确或不是账户登陆密码").setStatusCode(Constant.FAIL);
            }else if (result.equals("-21")) {
                resBuilder.setMsg("MD5接口密钥加密不正确").setStatusCode(Constant.FAIL);
            }else if (result.equals("-3")) {
                resBuilder.setMsg("短信数量不足").setStatusCode(Constant.FAIL);
            }else if (result.equals("-11")) {
                resBuilder.setMsg("该用户被禁用").setStatusCode(Constant.FAIL);
            }else if (result.equals("-14")) {
                resBuilder.setMsg("短信内容出现非法字符").setStatusCode(Constant.FAIL);
            }else if (result.equals("-4")) {
                resBuilder.setMsg("手机号格式不正确").setStatusCode(Constant.FAIL);
            }else if (result.equals("-41")) {
                resBuilder.setMsg("手机号码为空").setStatusCode(Constant.FAIL);
            }else if (result.equals("-42")) {
                resBuilder.setMsg("短信内容为空").setStatusCode(Constant.FAIL);
            }else if (result.equals("-51")) {
                resBuilder.setMsg("短信签名格式不正确").setStatusCode(Constant.FAIL);
            }else if (result.equals("-6")) {
                resBuilder.setMsg("IP限制").setStatusCode(Constant.FAIL);
            }
        }
        catch (Exception e)
        {         
            resBuilder.setMsg("出错啦！").setStatusCode(Constant.ERROR);
            e.printStackTrace();
        }if (post!=null)
        {
            post.releaseConnection();
        }
        
        return resBuilder.build();
    }
}
